﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class RapDAO
    {
        DataProvider dp = new DataProvider();

        public bool UpdateGioiHanTruocGioChieu(int _GioiHanTruocGioChieu)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@GioiHanTruocGioChieu", _GioiHanTruocGioChieu)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UpdateGioiHanTruocGioChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool AddGioiHanTruocGioChieu(int _GioiHanTruocGioChieu)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@GioiHanTruocGioChieu", _GioiHanTruocGioChieu)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_AddGioiHanTruocGioChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public int GetGioiHanTruocGioChieu()
        {
            if (dp.OpenConnect())
            {
                DataTable dt = dp.SqlExcuteQuery("uproc_GetGioiHanTruocGioChieu", null);
                dp.CloseConnect();
                if (dt.Rows.Count > 0)
                    return int.Parse(dt.Rows[0][0].ToString());
                return 60;
            }
            return -1;
        }
    
        public BindingList<RapDTO> GetList()
        {

            DataTable dt = new DataTable();
            BindingList<RapDTO> ds = new BindingList<RapDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Rap", null);
                foreach (DataRow row in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(row["maRap"].ToString());
                    Rap.TenRap = row["tenRap"].ToString();
                    Rap.DiaChi = row["diaChi"].ToString();
                    ds.Add(Rap);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<RapDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<RapDTO> ds = new BindingList<RapDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Rap_QL", null); 
                foreach (DataRow row in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(row["maRap"].ToString());
                    Rap.TenRap = row["tenRap"].ToString();
                    Rap.DiaChi = row["diaChi"].ToString();
                    Rap.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Rap);
                }
                dp.CloseConnect();
                return ds;  
            }
            return null;
        }

        public BindingList<RapDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<RapDTO> ds = new BindingList<RapDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Rap_QL_DaXoa", null);
                foreach (DataRow row in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(row["maRap"].ToString());
                    Rap.TenRap = row["tenRap"].ToString();
                    Rap.DiaChi = row["diaChi"].ToString();
                    Rap.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Rap);
                }
                dp.CloseConnect();
                return ds;  
            }
            return null;
        }

        public BindingList<RapDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<RapDTO> ds = new BindingList<RapDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_Rap_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(row["maRap"].ToString());
                    Rap.TenRap = row["tenRap"].ToString();
                    Rap.DiaChi = row["diaChi"].ToString();
                    Rap.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Rap);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<RapDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<RapDTO> ds = new BindingList<RapDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_Rap_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(row["maRap"].ToString());
                    Rap.TenRap = row["tenRap"].ToString();
                    Rap.DiaChi = row["diaChi"].ToString();
                    Rap.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Rap);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public RapDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_Rap", sqlParameter);
                RapDTO Rap = new RapDTO();
                foreach (DataRow row in dt.Rows)
                {

                    Rap.MaRap = int.Parse(row["maRap"].ToString());
                    Rap.TenRap = row["tenRap"].ToString();
                    Rap.DiaChi = row["diaChi"].ToString();
                }
                dp.CloseConnect();
                return Rap;
            }
            return null;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_Rap", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_Rap", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(RapDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@DiaChi", _DTO.DiaChi), 
                    new SqlParameter("@TenRap", _DTO.TenRap)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_Rap", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool CheckExists(int _Ma, string _Ten)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma), 
                    new SqlParameter("@TenRap", _Ten)
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_CheckExists_Rap", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count == 0)
                    return false;
                return true;// đã tồn tại
            }
            return false;
        }

        public bool Update(RapDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaRap", _DTO.MaRap), 
                    new SqlParameter("@DiaChi", _DTO.DiaChi), 
                    new SqlParameter("@TenRap", _DTO.TenRap)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_Rap", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}
